﻿using Kairos.Models;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Kairos.Views
{
    public static class ImageMapExtensions
    {
        public static MvcHtmlString ImageMap(this HtmlHelper htmlHelper, Image image)
        {
            return MvcHtmlString.Create(ImageMapExtensions.GenerateImageMapInternal(htmlHelper.ViewContext.HttpContext, image, (ICollection<Mappage>)new List<Mappage>(), (IDictionary<string, object>)new RouteValueDictionary()));
        }
        public static MvcHtmlString ImageMap(this HtmlHelper htmlHelper, Image image, ICollection<Mappage> mappages)
        {
            return MvcHtmlString.Create(ImageMapExtensions.GenerateImageMapInternal(htmlHelper.ViewContext.HttpContext, image, mappages, (IDictionary<string, object>)new RouteValueDictionary()));
        }
        public static MvcHtmlString ImageMap(this HtmlHelper htmlHelper, Image image, object htmlAttributes)
        {
            return MvcHtmlString.Create(ImageMapExtensions.GenerateImageMapInternal(htmlHelper.ViewContext.HttpContext, image, (ICollection<Mappage>)new List<Mappage>(), (IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
        }
        public static MvcHtmlString ImageMap(this HtmlHelper htmlHelper, Image image, ICollection<Mappage> mappages, object htmlAttributes)
        {
            return MvcHtmlString.Create(ImageMapExtensions.GenerateImageMapInternal(htmlHelper.ViewContext.HttpContext, image, mappages, (IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
        }
        public static MvcHtmlString ImageMap(this HtmlHelper htmlHelper, Image image, ICollection<Mappage> mappages, IDictionary<string, object> htmlAttributes)
        {
            return MvcHtmlString.Create(ImageMapExtensions.GenerateImageMapInternal(htmlHelper.ViewContext.HttpContext, image, mappages, htmlAttributes));
        }
        private static string GenerateImageMapInternal(HttpContextBase httpContext, Image image, ICollection<Mappage> mappages, IDictionary<string, object> htmlAttributes)
        {
            StringBuilder stringBuilder = new StringBuilder();

            TagBuilder tagBuilder;

            tagBuilder = new TagBuilder("img");
            tagBuilder.Attributes.Add("src", UrlHelper.GenerateContentUrl("~/Image/Data/" + image.ID, httpContext));
            tagBuilder.Attributes.Add("class", "imageMap");
            tagBuilder.Attributes.Add("usemap", "#map" + image.ID);
            tagBuilder.MergeAttributes<string, object>(htmlAttributes);
            stringBuilder.Append(tagBuilder.ToString());

            tagBuilder = new TagBuilder("map");
            tagBuilder.Attributes.Add("name", "map" + image.ID);
            stringBuilder.Append(tagBuilder.ToString(TagRenderMode.StartTag));

            foreach (Mappage mappage in mappages)
            {
                tagBuilder = new TagBuilder("area");
                tagBuilder.Attributes.Add("shape", "rect");
                tagBuilder.Attributes.Add("coords", mappage.Position);
                tagBuilder.Attributes.Add("href", mappage.Lien);
                tagBuilder.Attributes.Add("target", "_blank");
                stringBuilder.Append(tagBuilder.ToString());
            }

            tagBuilder = new TagBuilder("map");
            stringBuilder.Append(tagBuilder.ToString(TagRenderMode.EndTag));

            return stringBuilder.ToString();
        }
    }

}